//
//  SizeTableViewCell.m
//  帮惠Mall
//
//  Created by reew on 2017/9/23.
//  Copyright © 2017年 reew. All rights reserved.
//

#import "SizeTableViewCell.h"
#import "SizeCollectionViewCell.h"
#import "System.h"
@interface SizeTableViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *customSize;

@end
@implementation SizeTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.sizeCollectionView.delegate = self;
    self.sizeCollectionView.dataSource = self;
//    self.sizeCollectionView.scrollEnabled = NO;
    [self.sizeCollectionView registerNib:[UINib nibWithNibName:@"SizeCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"SizeCollectionViewCell"];
    if (_sizeArr == nil) {
        _sizeArr = [NSMutableArray array];
    }
    if (_selectedSizeArr == nil) {
        _selectedSizeArr = [NSMutableArray array];
    }
    
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _sizeArr.count+1;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SizeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SizeCollectionViewCell" forIndexPath:indexPath];
    if (indexPath.row == _sizeArr.count) {
        cell.customSize.hidden = NO;
        cell.size.hidden = YES;
        cell.customSize.delegate = self;
    }else{
        cell.customSize.hidden = YES;
        cell.size.hidden = NO;
        cell.size.text = self.sizeArr[indexPath.row];
    }
    
    
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((kScreenWidth-60)/3, 30);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 10, 5, 10);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    SizeCollectionViewCell *cell = (SizeCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if (cell.isSelected) {
        cell.backgroundColor = [UIColor whiteColor];
        [_selectedSizeArr removeObject:_sizeArr[indexPath.row]];
        cell.isSelected = NO;
    }else{
        cell.backgroundColor = [UIColor lightGrayColor];
        [_selectedSizeArr addObject:_sizeArr[indexPath.row]];
        cell.isSelected = YES;
    }
    //代理
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectSizeWithArray:)]) {
        [self.delegate didSelectSizeWithArray:[_selectedSizeArr copy]];
    }
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    //定义颜色
    if (!(textField.text == nil && textField.text.length ==0 )) {
        [_sizeArr addObject:textField.text];
        NSIndexPath *index = [NSIndexPath indexPathForRow:_sizeArr.count-1 inSection:0];
        [self.sizeCollectionView insertItemsAtIndexPaths:@[index]];
        //代理
        if (self.delegate && [self.delegate respondsToSelector:@selector(didAddSizeWithSize:)]) {
            [self.delegate didAddSizeWithSize:textField.text];
        }
    }
    textField.text = nil;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
}

@end
